package com.stable.web.controller;

import com.stable.service.RecordLevlesService;
import com.stable.utils.DateUtil;
import com.stable.utils.FileUtil;
import com.stable.vo.bus.RecordLevelsInfo;
import com.stable.vo.http.JsonResult;
import com.stable.vo.spi.req.EsQueryPageReq;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 突破新高查询
 */
@RequestMapping("/recordLevels")
@RestController
@Log4j2
public class RecordLevelsController {

    @Autowired
    private RecordLevlesService recordLevlesService;


    /**
     * 水平涨跌幅度最近7日统计
     *
     * @param forceJob          强制重新执行job，重新计算写入redis数据
     * @param startDate         日线数据过滤开始时间
     * @param endDate           日线数据过滤结束时间
     * @param code              单个股票代码，方便测试
     * @param recordLevelsRange 新高的涨幅范围                                  8%
     * @param backNdays         需要倒数N天的数据                               倒数5天(最近一周的所有新高)
     * @param excludeNearNdays  剔除最近N天计算新高                             剔除最近几天的价格开始计算
     * @param conceptName       概念关机键
     * @return
     */
    @RequestMapping(value = "/recordLevelsQuery", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> stocklist(@RequestParam(name = "forceJob", defaultValue = "false", required = false) Boolean forceJob,
                                                String queryDate,
                                                String startDate,
                                                String endDate,
                                                String code,
                                                @RequestParam(name = "recordLevelsRange", defaultValue = "8", required = false) int recordLevelsRange,
                                                @RequestParam(name = "backNdays", defaultValue = "5", required = false) int backNdays,
                                                @RequestParam(name = "excludeNearNdays", defaultValue = "7", required = false) int excludeNearNdays,
                                                @RequestParam(name = "sortBy250", defaultValue = "true", required = false) Boolean sortBy250,
                                                @RequestParam(name = "conceptName", defaultValue = "", required = false) String conceptName,
                                                EsQueryPageReq queryPage) {
        log.info("forceJob:{},queryDate:{},startDate:{},endDate:{},code:{},recordLevelsRange:{},backNdays:{},excludeNearNdays:{}", forceJob, queryDate, startDate, endDate, code, recordLevelsRange, backNdays, excludeNearNdays);
        queryPage.setPageSize(1000);
        if (StringUtils.isEmpty(queryPage.getSortName())) {
            queryPage.setSortName("tradeDate");
        }
        if (StringUtils.isEmpty(queryDate)) queryDate = DateUtil.getTodayYYYYMMDD();
        JsonResult r = new JsonResult();
        try {
            r.setResult(recordLevlesService.queryRecordLevels(forceJob, queryDate, startDate, endDate, code, queryPage, recordLevelsRange, backNdays, excludeNearNdays, conceptName, sortBy250));
            r.setStatus(JsonResult.OK);
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(JsonResult.ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }



    /**
     * 下载文件
     *
     * @return
     */
    @RequestMapping({"/download"})
    public void download(@RequestParam(name = "forceJob", defaultValue = "false", required = false) Boolean forceJob,
                         String queryDate,
                         String startDate,
                         String endDate,
                         String code,
                         @RequestParam(name = "recordLevelsRange", defaultValue = "8", required = false) int recordLevelsRange,
                         @RequestParam(name = "backNdays", defaultValue = "5", required = false) int backNdays,
                         @RequestParam(name = "excludeNearNdays", defaultValue = "7", required = false) int excludeNearNdays,
                         @RequestParam(name = "sortBy250", defaultValue = "true", required = false) Boolean sortBy250,
                         @RequestParam(name = "conceptName", defaultValue = "", required = false) String conceptName,
                         EsQueryPageReq queryPage) {
        queryPage.setPageSize(1000);
        if (StringUtils.isEmpty(queryPage.getSortName())) {
            queryPage.setSortName("tradeDate");
        }
        if (StringUtils.isEmpty(queryDate)) queryDate = DateUtil.getTodayYYYYMMDD();
        List<RecordLevelsInfo> recordLevelsInfos = recordLevlesService.queryRecordLevels(forceJob, queryDate, startDate, endDate, code, queryPage, recordLevelsRange, backNdays, excludeNearNdays, conceptName, sortBy250);

        String filename = "突破新高列表.txt";
        FileUtil.downloadFile(recordLevelsInfos.stream().map(RecordLevelsInfo::getCode).collect(Collectors.toList()), filename);
    }
}
